package com.kaka.bus.engine.amount.TopProduct;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.kaka.bus.R;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.DrainProductBean;
import com.kaka.bus.engine.home.main.h5.OtherH5Activity;
import com.kaka.bus.util.Test;

import java.util.List;

import butterknife.BindView;

public class TopProductActivity extends BaseActivity<TopPresenter> implements TopContract.TopView {
    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.btn_apply)
    Button btn_apply;
    @BindView(R.id.tv_amount)
    TextView tv_amount;
    @BindView(R.id.tv_date)
    TextView tv_date;
    @BindView(R.id.tv_dateRate)
    TextView tv_dateRate;
    @BindView(R.id.tv_apply_term)
    TextView tv_apply_term;
    @BindView(R.id.tv_description)
    TextView tv_description;
    @BindView(R.id.tv_baika_headline)
    TextView tv_baika_headline;
    @BindView(R.id.tv_baika_subhead)
    TextView tv_baika_subhead;
    @BindView(R.id.img_baika_icon)
    ImageView img_baika_icon;

    private DrainProductBean.ProductBean productBean;
    private String mProductId;
    private String mBannerId;
    public static final String PRODUCTBEAN = "productBean";
    public static final String PRODUCTID = "productId";
    public static final String BANNERID = "bannerId";

    @Override
    protected void initView() {
        super.initView();
        productBean = getIntent().getParcelableExtra(PRODUCTBEAN);
        mProductId = getIntent().getStringExtra(PRODUCTID);
        mBannerId = getIntent().getStringExtra(BANNERID);
        mToolbarBack.setOnClickListener(v -> finish());
        mToolbarName.setText(productBean.getProductName());
        tv_baika_headline.setText(productBean.getProductName());
        tv_baika_subhead.setText(productBean.getProductDescription());
        Glide.with(this).load(productBean.getProductIcon()).error(R.drawable.icon_kakadef).into(img_baika_icon);
        if (productBean.getRange().contains("万")) {
            tv_amount.setText(Test.lastWordzoomOut(productBean.getRange(), 10));
        } else {
            tv_amount.setText(productBean.getRange());
        }
        tv_dateRate.setText(TextUtils.isEmpty(productBean.getDayReat()) ? "0" : productBean.getDayReat());
        String date = productBean.getPeriods() + productBean.getUnit();
        if (date.contains("天") || date.contains("月") || date.contains("年")) {
            tv_date.setText(Test.lastWordzoomOut(date, 10));
        } else {
            tv_date.setText(date);
        }
        tv_apply_term.setText(null == productBean.getApplicationCondition() ? "" : productBean.getApplicationCondition().replace("\\n", "\n"));
        tv_description.setText(null == productBean.getAuditDesc() ? "" : productBean.getAuditDesc().replace("\\n", "\n"));
        LinearLayout linearLayout = findViewById(R.id.ll_contain);
        linearLayout.removeAllViews();

        List<DrainProductBean.ProductBean.MarksBean> marks =productBean.getMarks();
        if(marks.size() > 0){
            DrainProductBean.ProductBean.MarksBean marksBean = marks.get(0);
            String[] split = marksBean.getMarkDesc().split(",");
            for (int i = 0; i < split.length; i++) {

                // TODO 动态添加布局(java方式)
                LayoutInflater inflater = this.getLayoutInflater();
                LinearLayout view = (LinearLayout) inflater
                        .inflate(R.layout.item_lebal, null);
                TextView textView1 = view.findViewById(R.id.tv_target);
                textView1.setText(split[i].trim());
                linearLayout.addView(view);
            }
        }



        btn_apply.setOnClickListener(v -> {
            if (null != mProductId && null != mBannerId) {
                mPresenter.chargeApply(mProductId, mBannerId, getUserId(), "1");
            }

            Intent intent = new Intent(this, OtherH5Activity.class);
            intent.putExtra(OtherH5Activity.URL, productBean.getDirectUrl());
            intent.putExtra(OtherH5Activity.TITLE, productBean.getProductName());
            startActivity(intent);
        });
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_top_product;
    }

    @Override
    protected boolean needFullScreen() {
        return true;
    }
}
